package business_logic.game_elements;


import java.util.Collection;
import java.util.EnumSet;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;


import model.collisions.CollisionCategory;
import model.elements.Bomb;
import model.elements.attributes.Coordinate2D;
import model.elements.attributes.Damage;
import model.exceptions.ModelExceptions;

import business_logic.GameElement;
import business_logic.Tile;
import business_logic.view.GameViewIdentifier;

public class Molotov extends Bomb implements GameElement
{

	private static final CollisionCategory 	MolotovBlastType 	= CollisionCategory.C_L1_EXPLOTION;
	private static final Damage 			MolotovDamage		= new Damage(5);
	private static final int				MolotovTimeToLive	= 700;
	private static final int				MolotovBlastRange	= 3;
	
    private static final Collection<GameViewIdentifier> m_viewIdentifiers = 
            EnumSet.of(GameViewIdentifier.SPRITE_MOLOTOV);
    
	public Molotov(Coordinate2D position) throws ModelExceptions
	{
		super( position, 1.0, MolotovBlastType, MolotovDamage, MolotovBlastRange );
		super.setTTL(MolotovTimeToLive);
	}

	public Molotov(Coordinate2D position, double m_TTLModifier) throws ModelExceptions
	{
		super( position, m_TTLModifier, MolotovBlastType, MolotovDamage, MolotovBlastRange );
		super.setTTL(MolotovTimeToLive);
	}

	public Collection<GameViewIdentifier> getViewIdentifiers()
    {
        return m_viewIdentifiers;
    }

    @Override
    public void vivir()
    {
        try
        {
            this.update();
        }
        catch (ModelExceptions e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public int getX()
    {
        return Tile.pixelToTile( m_currentPosition.getRealX() );
    }

    @Override
    public int getY()
    {
        return Tile.pixelToTile( m_currentPosition.getRealY() );
    }

    private static final long m_serialVersionUID = 1L;
    
    @Override
    public Element save()
    {
        Element elem = DocumentHelper.createElement( this.getClass().getName() );
        elem.addAttribute( "serialVersion", m_serialVersionUID+"" );
        elem.addAttribute( "x", Math.floor( this.getPosition().getRealX() ) +"" );
        elem.addAttribute( "y", Math.floor( this.getPosition().getRealY() ) +"" );
        elem.addAttribute( "ttl", this.m_ttl.getValue()+"" );
        elem.addAttribute( "ttl_modifier", this.m_ttl.getModifier()+"" );
        
        return elem;
    }
    
    public Molotov( Element elem ) throws ModelExceptions
    {
        super
        (
            new Coordinate2D
            (
                Double.parseDouble( elem.attributeValue("x") ),
                Double.parseDouble( elem.attributeValue("y") )
            ),
            Double.parseDouble( elem.attributeValue("ttl_modifier") ),
            MolotovBlastType,
            MolotovDamage,
            MolotovBlastRange
        );
        
        super.setTTL( Integer.parseInt( elem.attributeValue("ttl") ) );
    }
    
}
